<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="vue.js"></script>
    <title>Document</title>
    <style>
        table, td, th{
            border-collapse: collapse;
            border: 1px solid #ccc;
            text-align: center;
        }

        td, th {
            line-height: 36px;
            width: 100px;
        }
    </style>
</head>
<body>
    <main id="app">
        <table>
            <tr>
                <th>商品名称</th>
                <th>商品价格</th>
                <th>商品数量</th>
                <th>操作</th>
            </tr>
            <tr v-for="(goods,index) in goodsList">
                <td>{{goods.goodName}}</td>
                <td>{{goods.price}}</td>
                <td><button @click="preClick(index)" >-</button><span>{{goods.goodCount}}</span><button @click="nextClick(index)">+</button></td>
                <td><button @click="goodsList.splice(index,1)">移除</button></td>
            </tr>
        </table>
        <first-com :name='name' :address="address" :thirdcount="thirdCount" ></first-com>
    </main>

   <script id="first" type="text/html">
        <section>
            <input type="text" v-model="name">
            <input type="text" v-model="address">
            <div>
                <h1>清单</h1>
                <div>商品总价：{{thirdcount}}</div>
                <div>收件人：{{name}}</div>
                <div>收货地址：{{address}}</div>
            </div>
        </section>
   </script>
   
    <script>
        Vue.component('first-com',{
            template:'#first',
            data:function(){
                return {
                    newname:this.name,
                    newaddress:this.address,
                    newthirdcount:this.thirdcount,
                }
            },
            props:['name','address','thirdcount']
        })


        var vue=new Vue({
            el:'#app',
            data:{
                name:'小美',
                address:'郑州',
                firstCount:'',
                secondCount:'',
                thirdCount:'8',
                goodsList:[
                    {
                        goodName:'辣条',
                        price:1,
                        goodCount:2,
                        isRed:true
                    },
                    {
                        goodName:'泡椒鸡爪',
                        price:1,
                        goodCount:2,
                        isRed:true
                    },{
                        goodName:'巧克力',
                        price:2,
                        goodCount:2,
                        isRed:true
                    }
                ]
            },
            methods:{
                preClick(val){
                    console.log(val)
                    if(this.goodsList[val].goodCount==0){
                        return
                    }
                    this.goodsList[val].goodCount--
                    this.firstCount=this.goodsList[val].goodCount
                    this.firstCount=this.allPrice
                    
                },
                nextClick(val){
                   if(this.goodsList[val].goodCount==10){
                      return
                   }
                   this.goodsList[val].goodCount++
                   this.secondCount=this.goodsList[val].goodCount
                   this.secondCount=this.allPrice
                }
            },
            computed:{
                allPrice(){
                    var temp=0
                    for(var index=0;index<this.goodsList.length;index++){
                       temp+=(this.goodsList[index].price)*(this.goodsList[index].goodCount)
                    }
                    this.thirdCount=temp
                    console.log(this.thirdCount)
                    return this.thirdCount
                }
            }
        })
    </script>
</body>
</html>